package com.xiaomo.gitee.datamanager.controller;

import com.xiaomo.gitee.datamanager.common.result.Result;
import com.xiaomo.gitee.datamanager.dto.menu.DataMenuDTO;
import com.xiaomo.gitee.datamanager.mapper.menu.DataMenuMapper;
import com.xiaomo.gitee.datamanager.mapper.menu.MainMenuMapper;
import com.xiaomo.gitee.datamanager.entity.menu.MainMenuPO;
import com.xiaomo.gitee.datamanager.service.menu.DataMenuService;
import com.xiaomo.gitee.datamanager.service.menu.impl.DataMenuServiceImpl;
import com.xiaomo.gitee.datamanager.util.AuthorizeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
@Tag(name = "动态菜单",description = "根据权限动态返回菜单")
@RequiredArgsConstructor
public class MenuController {

    private final MainMenuMapper mainMenuMapper;
    private final DataMenuService dataMenuService;

    @GetMapping("/main-menu")
    @PreAuthorize("permitAll()")
    @Operation(
            summary = "获取主菜单",
            description = "获取主菜单列表接口",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "获取成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    array = @ArraySchema(
                                            schema = @Schema(
                                                    implementation = MainMenuPO.class
                                            )
                                    )
                            )
                    )
            }
    )
    public Result<?> getMainMenu() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!authentication.isAuthenticated()) {
            return Result.ok(mainMenuMapper.selectAll().stream().filter(column -> column.getPerm() == null || column.getPerm().isEmpty()));
        } else {
            List<String> permissionList = AuthorizeUtils.getPermissions();
            return Result.ok(mainMenuMapper.selectAll()
                    .stream().filter(column -> {
                        String perm = column.getPerm();
                        return (perm == null || perm.isEmpty()) || permissionList.contains(perm);
                    }).toList()
            );
        }

    }

    @GetMapping("/data-menu")
    @PreAuthorize("hasRole('user')")
    @Operation(
            summary = "获取数据管理页面菜案",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "成功",
                            content = @Content (
                                    mediaType = "application",
                                    array = @ArraySchema(
                                            schema = @Schema(
                                                    implementation = DataMenuDTO.class
                                            )
                                    )
                            )
                    )
            }
    )
    public Result<?> getDataMenu() {
        return Result.ok(dataMenuService.resolveMenu());
    }

}
